﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using iPublic;
using Newtonsoft.Json;

namespace HRecycle.SysTools
{
    /// <summary>
    /// 输入会员号，海宏软件
    ///   1：后台网页中做了个微信登陆的二维码，在这里通过webBroswer显示。
    ///      http://h.sdzgwl.cn/webapi.php/wxlogin/index 
    ///   2：用户微信扫描后，授权，微信官方将跳转浏览器链接到：
    ///      http://www.aigou198.com/webapi.php/wxlogin/login?code=011XVnsQ1mggw91pSgpQ13XhsQ1XVnsa&state=500
    ///   3：拦截住这个请求，用httpGet请求之
    /// 相关参考：
    ///   webBrow的非常规编程：serhttps://www.cnblogs.com/sexintercourse/p/6063512.html
    ///   
    /// 修改记录：
    ///   20171214，zch，创建基本功能
    /// </summary>
    public partial class FrmInputVIP : Form
    {
        public FrmInputVIP()
        {
            InitializeComponent();
        }

        public static string sVipID = "";
        private string sDefaultUrl = "http://h.sdzgwl.cn/webapi.php/wxlogin/index";
        public string sDefaultText = "";


        private void FrmInputVIP_Load(object sender, EventArgs e)
        {
            try
            {
                showInfo("★★进入会员号输入单元★★");
                //界面
                FormBorderStyle = FormBorderStyle.None;
                WindowState = FormWindowState.Maximized;
                //
                pl_bottom2.Dock = DockStyle.Bottom;
                pl_bottom3.Dock = DockStyle.Bottom;
                lbl_machineID.Text = iDefine.SysConfig.machineID;
                pl_main.Dock = DockStyle.Fill;
                //wb_main.Dock = DockStyle.Fill;
                //
                sDefaultText = txt_vip.Text;
                //txt_vip.Focus();
                //txt_vip.SelectedText = sDefaultText;
                tm_runonce.Enabled = true;
            }
            catch (Exception x)
            { showInfo(x.Message); }
        }


        public void showInfo(string sInfo, bool lTime = true)
        {
            lbl_info.Text = sInfo;
            iLog.writeLogFile(sInfo);
        }

        private void wb_main_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            showInfo("Navigated：" + e.Url.ToString());
        }

        private void wb_main_Navigating(object sender, WebBrowserNavigatingEventArgs e)
        {   //拦截跳转
            iDefine.ChuLiJieGuo jg = null;
            string s = "", sUrl = e.Url.ToString();
            try
            {
                showInfo("Navigating：" + sUrl);
                //扫描授权后跳转：http://www.aigou198.com/webapi.php/wxlogin/login?code=011XVnsQ1mggw91pSgpQ13XhsQ1XVnsa&state=500
                if (sUrl.ToUpper().Contains("webapi.php/wxlogin/login?code=".ToUpper()))
                {
                    e.Cancel = true;
                    if ((jg = iPublicX.httpGet(sUrl, ref s)).error != 0) throw new Exception("请求出错：" + jg.error.ToString() + "," + jg.message);
                    showInfo(s);
                    //分析
                    iDefine.weixinInpuUserResult wx = JsonConvert.DeserializeObject<iDefine.weixinInpuUserResult>(s);
                    if (wx == null) throw new Exception("返回字符串无效，非weixinInpuUserResult标准格式！");
                    if (wx.result.ToUpper() != "TRUE") throw new Exception("返回值无效！" + wx.result);
                    sVipID = wx.user_id;                //取到了数值
                    if (string.IsNullOrEmpty(sVipID)) throw new Exception("user_id无效！");
                    //关闭窗口，返回值
                    txt_vip.Text = sVipID;
                    btn_vip_Click(sender, e);
                }
            }
            catch (Exception x)
            {
                showInfo(x.Message);
            }
        }

        private void btn_vip_Click(object sender, EventArgs e)
        {
            if (txt_vip.Text == "") { txt_vip.Focus(); return; }
            Close();
            DialogResult = DialogResult.OK;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            string s = "您投入了一堆东西，然后不要积分就走了，您的高风亮节我们非常感谢和敬佩！";
            showInfo(s);
            iPublicX.showMessage(s);
            Close();
            DialogResult = DialogResult.Cancel;
        }

        private void FrmInputVIP_Shown(object sender, EventArgs e)
        {
            tm_main.Tag = Convert.ToInt16(lbl_time.Text);
            tm_main.Enabled = true;
            tm_task.Enabled = true;
        }

        private void tm_main_Tick(object sender, EventArgs e)
        {
            Timer tm = sender as Timer;
            bool lEnable = tm.Enabled;
            tm.Enabled = false;
            //
            int nTime = Convert.ToInt16(lbl_time.Text) - 1;
            lbl_time.Text = nTime.ToString();
            if (nTime <= 0) btn_cancel_Click(sender, e); else tm.Enabled = lEnable;
        }

        private void tm_task_Tick(object sender, EventArgs e)
        {
            lbl_date.Text = DateTime.Now.ToString("yyyy-MM-dd");
            lbl_shifenmiao.Text = DateTime.Now.ToString("HH:mm");
        }

        private void btn_continue_Click(object sender, EventArgs e)
        {
            DialogResult = DialogResult.Retry;
            Close();
        }

        private void FrmInputVIP_Resize(object sender, EventArgs e)
        {
            int nW = this.Width, n = nW / 2;
            pl_gongzhonghao.Width = n;
            wb_main.Dock = DockStyle.Fill;
            wb_main.Width = n;
        }

        private void tm_runonce_Tick(object sender, EventArgs e)
        {
            tm_runonce.Enabled = false;
            wb_main.Url = new Uri(sDefaultUrl);
        }

        private void txt_vip_Click(object sender, EventArgs e)
        {
            if (txt_vip.Text == sDefaultText)
            {
                txt_vip.Text = "";
                txt_vip.ForeColor = Color.Black;
            }
        }
    }

}
